	
	var url="/tbpoject/user/";
	var pc=1;
	$(function(){
		getAll(pc);
	})

	// 查询所有用户
	function getAll(pc){
		$.getJSON(url+"getAll",{"pageCode":pc},function(data){
			var tbody=$("#tbody");
				tbody.html("");
				$(".pagination").html("");
			for(var i=0;i<data.beanList.length;i++){
				tbody.append("<tr>" 
							+"<td>"+data.beanList[i].id+"</td>"
							+"<td>"+data.beanList[i].name+"</td>"
							+"<td>"+data.beanList[i].sex+"</td>"
							+"<td>"+data.beanList[i].birthday+"</td>"
							+"<td>"+data.beanList[i].number+"</td>"
							+"<td>"+data.beanList[i].level+"</td>"
							+"<td>"+data.beanList[i].time+"</td>"
							+"<td style='display:none;'>"+data.beanList[i].hasStore+"</td>"
							+"<td>"
							+'<a href="javascript:updateP('+data.beanList[i].id+','+data.pc+')">编辑</a>&nbsp;&nbsp;&nbsp;'
							+'<a href="javascript:deleteU('+data.beanList[i].id+','+data.pc+')">删除</a>'
							+"</td>"
							+"</tr>")
			}
			$(".pagination").html(getPage(data,"getAll"));
		})
	}
	
	
	function getUserByName(){
		//alert(111)
		var name=$("#username").val();
		//alert(name)
		
		$.post(url+"getUserByName",{"name":name},function(data){
			if(data=="error"){
				var tbody=$("#tbody");
				tbody.html("");
				$(".pagination").html("");
				tbody.append("<tr><td>数据为空!!</td></tr>");
			}else{
				
			var items=$.parseJSON(data);
			var tbody=$("#tbody");
			tbody.html("");
			$(".pagination").html("");
			for(var i=0;i<items.length;i++){
				tbody.append("<tr>" 
							+"<td>"+items[i].id+"</td>"
							+"<td>"+items[i].name+"</td>"
							+"<td>"+items[i].sex+"</td>"
							+"<td>"+items[i].birthday+"</td>"
							+"<td>"+items[i].number+"</td>"
							+"<td>"+items[i].level+"</td>"
							+"<td>"+items[i].time+"</td>"
							+"<td style='display:none'>"+items[i].hasStore+"</td>"
							+"<td>"
							+'<a href="javascript:updateT('+items[i].id+')">编辑</a>&nbsp;&nbsp;&nbsp;'
							+'<a href="javascript:delU('+items[i].id+')">删除</a>'
							+"</td>"
							+"</tr>")
			}
			}
		})
	}
	
	//通过姓名查询的编辑跳转
	function updateT(id){
		window.location=url+"updateT?id="+id;
	}
	
	
	//分页上的编辑跳转
	function updateP(id,page){
		window.location=url+"updateT?id="+id+"&pc="+page;
	}
	
	//修改用户信息
	function updateUser(){
		pc=$("#pc").val();//得到当前页码
		var id=$("#id").val();
		var name=$("#name").val();
		var sex=$("#sex").val();
		var birth=$("#birthday").val();
		var number=$("#number").val();
		var level=$("#level").val();
		var hasStore=$("#hasStore").val();
		$.post(url+"updateUser",{
						"id":id,"name":name,
						"sex":sex,"birthday":birth,
						"number":number,"level":level,
						"hasStore":hasStore},function(data){
					//alert(data)
					if(data=="success"){
						swal("修改成功!", "用户信息已修改", "success");
						
					}else{
						swal("修改失败!", "用户信息修改失败", "error");
					}
				
					//window.location="/tbpoject/background/usermanager/userManager.jsp";
		})
		
	}
	
	//分页删除用户
	var uid;
	var upIndex;
	function deleteU(id,pageIndex){
		uid=id;
		upIndex=pageIndex;
		swal({
			title: "确定要删除吗？",
			text: "删除后，数据不能恢复",
			type: "warning",
			showCancelButton: true,
			cancelButtonText:'取消',
			confirmButtonColor: '#DD6B55',
			confirmButtonText: '确定',
			closeOnConfirm: false
		},
		function(isConfirm){
			if(isConfirm){
				deleteUser();
			}
				
		});
	}
	
	//分页删除
	function deleteUser(){
		var id=uid;
		pc=upIndex;
		$.post(url+"deleteUser",{"id":id},function(data){
				if(data=="success"){
					swal("删除成功!", "用户信息已删除", "success");
					getAll(pc);
				}else{
					swal("删除失败!", "该用户信息不存在", "error");
				}
		})
	}
	
	//通过姓名查询的删除用户
	var nid;
	function delU(id){
		nid=id;
		swal({
			title: "确定要删除吗？",
			text: "删除后，数据不能恢复",
			type: "warning",
			showCancelButton: true,
			cancelButtonText:'取消',
			confirmButtonColor: '#DD6B55',
			confirmButtonText: '确定',
			closeOnConfirm: false
		},
		function(isConfirm){
			if(isConfirm){
				delUser();
			}
				
		});
	}
	//通过查询用户名的删除
	function delUser(){
		var id =nid
		$.post(url+"deleteUser",{"id":id},function(data){
			if(data=="success"){
				swal("删除成功!", "用户信息已删除", "success");
				getAll(pc);
			}else{
				swal("删除失败!", "该用户信息不存在", "error");
			}
		})
	}
	
	
